package com.xjh.him.controller;

import com.xjh.him.annotation.LoginVerify;
import com.xjh.him.dto.trend.TrendDTO;
import com.xjh.him.dto.trend.TrendPublishDTO;
import com.xjh.him.service.TrendServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.PageDTO;
import org.xjh.tool.dto.ResultDTO;
import org.xjh.tool.vo.AjaxResultVO;
import org.xjh.tool.vo.PageVO;

import java.util.List;

@RestController
@RequestMapping("/trend")
public class TrendController {

    @Autowired
    private TrendServiceImpl trendService;


    @LoginVerify
    @RequestMapping("/list")
    public AjaxResultVO listTrend(@RequestBody PageVO pageVO) {
        if (Checks.isAnyEmpty(pageVO, pageVO.getPage(), pageVO.getPerPageSize())) {
            return AjaxResultVO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setLimit(pageVO.getPerPageSize());
        pageDTO.setSkip((pageVO.getPage() - 1) * pageVO.getPerPageSize());
        ResultDTO<List<TrendDTO>> resultDTO = trendService.listTrend(pageDTO);
        if (resultDTO.isSuccess()) {
            return AjaxResultVO.getSuccessResult(resultDTO.getResult());
        }
        return AjaxResultVO.getFailureResult(resultDTO.getErrMsg());
    }

    @LoginVerify
    @RequestMapping("/publish")
    public AjaxResultVO publishTrend(@RequestBody TrendPublishDTO publishDTO) {
        ResultDTO<Boolean> resultDTO = trendService.publishTrend(publishDTO);
        if (resultDTO.isSuccess()) {
            return AjaxResultVO.getSuccessResult(resultDTO.getResult());
        }
        return AjaxResultVO.getFailureResult(resultDTO.getErrMsg());
    }
}
